package weiyao.xinxidasai.mapper;

import org.apache.ibatis.annotations.*;
import weiyao.xinxidasai.DTO.FilePO;
import weiyao.xinxidasai.pojo.FFiles;

import java.util.List;

@Mapper
public interface FilesMapper {
    @Select("SELECT * FROM files WHERE folder_id = #{folderId}")
    List<FFiles> findFilesByFolderId(Long folderId);

    @Insert("INSERT INTO files (files_id, user_id, folder_id, display_name, oss_no, file_size, mime_type, upload_time, version_id,files_lock,file_md5,file_type,files_token,Encryption_type) " +
            "VALUES (#{filesId}, #{userId}, #{folderId}, #{displayName}, #{ossNo}, #{fileSize}, #{mimeType}, now(), #{versionId},0,#{fileMd5},#{fileType},#{filesToken},#{EncryptionType})")
    @Options(useGeneratedKeys = true, keyProperty = "filesId")
    int insert(FFiles FFiles);
    @Insert("INSERT INTO files (files_id, user_id, folder_id, display_name,  file_size,  upload_time, version_id,files_lock,file_md5,file_type) " +
            "VALUES (#{filesId}, #{userId}, #{folderId}, #{displayName}, #{fileSize}, now(), #{versionId},0,#{fileMd5},#{fileType})")
    @Options(useGeneratedKeys = true, keyProperty = "filesId")
    int insertexcludemimetype(FFiles FFiles);
    @Insert("INSERT INTO files (files_id, user_id, folder_id, display_name, file_size, mime_type, upload_time, version_id,files_lock,file_md5,file_type,files_token,Encryption_type) " +
            "VALUES (#{filesId}, #{userId}, #{folderId}, #{displayName},  #{fileSize}, #{mimeType}, now(), #{versionId},0,#{fileMd5},#{fileType},#{filesToken},#{EncryptionType})")
    @Options(useGeneratedKeys = true, keyProperty = "filesId")
    int insertexcludeossNo(FFiles FFiles);


    @Select("SELECT * FROM files WHERE files_id = #{filesId}")
    FFiles getFileById(Long filesId);


    // 根据文件ID查询文件
    @Select("SELECT * FROM files WHERE files_id = #{filesId}")
    FFiles selectFileById(Long filesId);



    @Delete("DELETE FROM files WHERE files_id = #{filesId}")
    int deleteFile(Long filesId);

    @Update("UPDATE files SET display_name = #{newName} " +
            "WHERE files_id = #{filesId}")
    int updateFileName(@Param("filesId") Long filesId,
                       @Param("newName") String newName);

    @Select("SELECT COUNT(*) > 0 FROM files " +
            "WHERE display_name = #{fileName} AND folder_id = #{folderId}")
    boolean existsByFileNameAndFolderId(@Param("fileName") String fileName,
                                        @Param("folderId") Long folderId);


    @Select("SELECT * FROM files WHERE user_id = #{userId}")
    List<FFiles> findByUserId(@Param("userId") Long userId);

    @Select("SELECT * FROM files WHERE folder_id = #{folderId}")
    List<FFiles> findByFolderId(@Param("folderId") Long folderId);

    @Select("SELECT * FROM files " +
            "WHERE user_id = #{userId} AND " +
            "(display_name LIKE CONCAT('%', #{keyword}, '%') OR " +
            " mime_type LIKE CONCAT('%', #{keyword}, '%'))")
    List<FFiles> searchByUserIdAndKeyword(
            @Param("userId") Long userId,
            @Param("keyword") String keyword);

    @Select("SELECT * FROM files WHERE version_id = #{versionId}")
    List<FFiles> findFilesByversionId(Long versionId);

    //文件是否属于该用户
    @Select("SELECT files_id FROM files WHERE user_id = #{userId} AND files_id=#{filesId}")
    FFiles getFileByuserId(@Param("userId") Long userId, @Param("filesId") Long filesId);

    //文件上锁
    @Update("UPDATE files SET files_lock = 1, files_password=#{filesPassword} " +
            "WHERE files_id = #{filesId}")
    int FileLock(@Param("filesId") Long filesId, @Param("filesPassword") String filesPassword);

    //文件解锁
    @Update("UPDATE files SET files_lock = 0, files_password = NULL " +
            "WHERE files_id = #{filesId}")
    int FileUnLock(@Param("filesId") Long filesId);


    @Update("UPDATE files SET folder_id = #{targetFolderId} WHERE files_id = #{filesId}")
    int updateFileFolder(@Param("filesId") Long filesId,
                         @Param("targetFolderId") Long targetFolderId);

    @Select("SELECT files_id FROM files WHERE file_md5=#{md5}")
    FFiles selectFileByMd5(String md5);

    // 更新文件MD5
    @Update("UPDATE files SET file_md5 = #{md5} WHERE files_id = #{fileId}")
    void updateFileMd5(@Param("fileId") Long fileId, @Param("md5") String md5);
    // 更新文件大小
    @Update("UPDATE files SET file_size = #{fileSize} WHERE files_id = #{filesId}")
    void updateFileSize(@Param("filesId") Long filesId,@Param("fileSize") long fileSize);
    // 更新存储空间
    @Update("UPDATE files SET oss_no = #{ossNo} WHERE files_id = #{filesId}")
    void updateOssNo(@Param("filesId") Long filesId,@Param("ossNo") String ossNo);
    @Select("SELECT * FROM files WHERE file_md5=#{md5}")
    FFiles selectByMd5(String md5);
    @Update("UPDATE files SET files_lock = 1 WHERE files_id = #{filesId}")
    int FileLockByFace(Long filesId);

    @Select("SELECT * FROM files WHERE file_md5 = #{md5} AND user_id = #{userId} AND oss_no IS NULL")
    FFiles selectUploadingFileByMd5AndUser(@Param("md5") String md5, @Param("userId") Long userId);
    @Select("SELECT * FROM files WHERE file_md5 = #{md5}")
    List<FFiles> selectFilesByMd5(String md5);
}